#include "head.h"

typedef struct train_s{
    int length;
    char buf[1024];
}train_t;

void command_puts(command_t* command,int socketFd){
    printf("文件已经开始传输\n");
    char* file_name = "2.txt";
    /*从command中获取文件名字*/
    int file_fd = open(file_name,O_RDWR);
    ERROR_CHECK(file_fd,0,"open");

    int name_len = strlen(file_name);//发送文件名字和长度
    send(socketFd,&name_len,sizeof(int),MSG_NOSIGNAL);
    printf("已经发送的文件名字长度是 %d\n",name_len);
    send(socketFd,file_name,name_len,MSG_NOSIGNAL);
    printf("发送的文件名字内容是 %s\n",file_name);

    struct stat stat_file;
    fstat(file_fd,&stat_file);

    send(socketFd,&stat_file.st_size,sizeof(int),MSG_NOSIGNAL);
    printf("发送的文件内容长度是 %ld\n",stat_file.st_size);

    int serverfile_len;
    recv(socketFd,&serverfile_len,sizeof(int),MSG_NOSIGNAL);//用来告知客户端已经有多少数据了
    printf("客户端已经接收的长度是 %d\n",serverfile_len);
    
    off_t offset = 0;//从偏移量开始进行传送
    offset = (off_t) serverfile_len;

    int clientfile_len = stat_file.st_size;
    if(serverfile_len == 0){
        sendfile(socketFd,file_fd,NULL,clientfile_len);
        ERROR_CHECK(socketFd,0,"sendfile");
    }else{
        printf("需要传送文件的长度是%ld\n",stat_file.st_size - serverfile_len);
        sendfile(socketFd,file_fd,&offset,stat_file.st_size - serverfile_len);
    }

    return;

}

